package com.panopset.centralen.resource;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Favicon 16 x 16 image. Uses a semi-singleton pattern, where if you simply do
 * a new Favicon("/com/mycompany/icons/my16x16logo.png") at the beginning of
 * your program, subsequent calls to Favicon.getLatest() will provide your
 * favicon ImageResource. The default favicon is a Panopset icon.
 *
 * @see ImageResource
 *
 * @author Karl Dinwiddie
 *
 */
public final class Favicon {

    /**
     * Path to favicon.
     */
    private final String path;

    /**
     * Favicon constructor. This sets the application Favicon to the new image
     * provided in packagePath.
     *
     * @param packagePath
     *            Image must be a Java package 16 x 16 image.
     */
    public Favicon(final String packagePath) {
        path = packagePath;
        setLatest(this);
    }

    /**
     * Image resource for favicon.
     */
    private ImageResource imageResource;

    /**
     * Get image resource for this Favicon.
     *
     * @return ImageResource.
     */
    public ImageResource getImageResource() {
        if (imageResource == null) {
            imageResource = new ImageResource(getPath());
        }
        return imageResource;
    }

    /**
     * Latest Favicon, following a semi-singleton pattern.
     */
    private static volatile Favicon latest;

    /**
     * Semi singleton pattern set latest favicon. Should only be called once.
     *
     * @param newFavicon
     *            New favicon.
     */
    private void setLatest(final Favicon newFavicon) {
        if (latest != null) {
            Logger.getAnonymousLogger().log(Level.FINE,
                    "Favicon set more than once.");
        }
        latest = newFavicon;
    }

    /**
     * Semi-singleton pattern accessor.
     *
     * @return latest Favicon.
     */
    public static Favicon getLatest() {
        if (latest == null) {
            latest = new Favicon(PANOPSET_ICON_PATH);
        }
        return latest;
    }

    /**
     * Get path.
     *
     * @return package path.
     */
    public String getPath() {
        return path;
    }

    /**
     * /com/panopset/centralen/resource/panopset16x16.png.
     */
    public static final String PANOPSET_ICON_PATH
     = "/com/panopset/centralen/resource/panopset16x16.png";
}
